//
//  JHZTabBar.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/5/31.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "JHZTabBar.h"


@interface JHZTabBar ()

@property (nonatomic, strong) UIButton *plusBtn;

@end

@implementation JHZTabBar


@synthesize delegate;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
        
       
        CGRect newFrame = plusBtn.frame;
        newFrame.size = plusBtn.currentBackgroundImage.size;
        plusBtn.frame = newFrame;
        
        [plusBtn addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:plusBtn];
        self.plusBtn = plusBtn;
    }
    return self;
}


- (void)plusBtnClick {
    // 通知代理
    if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
        [self.delegate tabBarDidClickPlusButton:self];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 1.设置加号按钮的位置
    self.plusBtn.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    
    
    // 2.设置其他tabbarButton的frame
    CGFloat tabBarButtonW = self.frame.size.width / 5;
    CGFloat tabBarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            // 设置x
            CGRect newFrame = child.frame;
            newFrame.origin.x = tabBarButtonIndex * tabBarButtonW;
            // 设置宽度
            newFrame.size.width = tabBarButtonW;
            child.frame = newFrame;
            // 增加索引
            tabBarButtonIndex++;
            if (tabBarButtonIndex == 2) {
                tabBarButtonIndex++;
            }
        }
    }
}

@end
